package com.powernode.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ChangeStock;
import com.powernode.domain.Prod;
import com.powernode.domain.Sku;
import com.powernode.service.ProdService;
import com.powernode.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品接口管理")
@RestController
@RequestMapping("/prod/prod")
public class ProdController {

    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;

    @ApiOperation("多条件分页查询商品列表")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('prod:prod:page')")
    public ResponseEntity<Page<Prod>> loadProdPage(Page<Prod> page, Prod prod) {
        prodService.selectProdPage(page, prod);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('prod:prod:save')")
    public ResponseEntity<Void> saveProd(@RequestBody Prod prod) {
        prodService.save(prod);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("根据标题查询商品详情")
    @GetMapping("/info/{prodId}")
    @PreAuthorize("hasAnyAuthority('prod:prod:info')")
    public ResponseEntity<Prod> loadProdInfo(@PathVariable("prodId") Long prodId) {
        Prod prod = prodService.getById(prodId);
        return ResponseEntity.ok(prod);
    }

    @ApiOperation("修改商品信息")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('prod:prod:update')")
    public ResponseEntity<Void> update(@RequestBody Prod prod) {
        prodService.updateById(prod);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("批量删除商品")
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('prod:prod:delete')")
    public ResponseEntity<Void> deleteProd(@RequestBody List<Long> prodIds) {
        prodService.removeByIds(prodIds);
        return ResponseEntity.ok().build();
    }

    ///////////////////// 远程接口调用 ///////////////
    @GetMapping("/getProdById")
    // @PreAuthorize("hasAnyAuthority('prod:prod:page')")
    public Prod getProdById(@RequestParam Long prodId) {
        return prodService.getById(prodId);
    }


    @GetMapping("/selectProdListByIds")
    List<Prod> selectProdListByIds(@RequestParam("ids") List<Long> ids) {
        return prodService.selectProdListByIds(ids);
    }

    @GetMapping("/getSkuListBySkuIds")
    List<Sku> getSkuListBySkuIds(@RequestParam List<Long> ids) {
        return skuService.listByIds(ids);
    }

    @PostMapping("/changeStock")
    void changeStock(@RequestBody ChangeStock changeStock) {
        prodService.changeStock(changeStock);
    }

    ////////////// 前台 //////////////////////////
    @ApiOperation("根据商品标识查询商品详情和sku列表")
    @GetMapping("/prod/prodInfo")
    public ResponseEntity<Prod> loadProdAndSkuList(@RequestParam Long prodId) {
        Prod prod = prodService.selectProdAndSkuList(prodId);
        return ResponseEntity.ok(prod);
    }



}
